<?php
class Image_Requirement extends QDB_ActiveRecord_Abstract
{
	/**
	 * 返回对象的定义
	 *
	 * @static
	 *
	 * @return array
	 */
	static function __define()
	{
		return array
		(
			// 指定该 ActiveRecord 要使用的行为插件
			'behaviors' => '',

			// 指定行为插件的配置
			'behaviors_settings' => array
			(
				# '插件名' => array('选项' => 设置),
			),

			// 用什么数据表保存对象
			'table_name' => 'image_requirement',

			// 指定数据表记录字段与对象属性之间的映射关系
			// 没有在此处指定的属性，QeePHP 会自动设置将属性映射为对象的可读写属性
			'props' => array
			(
				// 主键应该是只读，确保领域对象的“不变量”
				'id' => array('readonly' => true),

				/**
				 *  可以在此添加其他属性的设置
				 */
				# 'other_prop' => array('readonly' => true),

				/**
				 * 添加对象间的关联
				 */
				# 'other' => array('has_one' => 'Class'),

			),

			/**
			 * 允许使用 mass-assignment 方式赋值的属性
			 *
			 * 如果指定了 attr_accessible，则忽略 attr_protected 的设置。
			 */
			'attr_accessible' => '',

			/**
			 * 拒绝使用 mass-assignment 方式赋值的属性
			 */
			'attr_protected' => 'id',

			/**
			 * 指定在数据库中创建对象时，哪些属性的值不允许由外部提供
			 *
			 * 这里指定的属性会在创建记录时被过滤掉，从而让数据库自行填充值。
			 */
			'create_reject' => '',

			/**
			 * 指定更新数据库中的对象时，哪些属性的值不允许由外部提供
			 */
			'update_reject' => '',

			/**
			 * 指定在数据库中创建对象时，哪些属性的值由下面指定的内容进行覆盖
			 *
			 * 如果填充值为 self::AUTOFILL_TIMESTAMP 或 self::AUTOFILL_DATETIME，
			 * 则会根据属性的类型来自动填充当前时间（整数或字符串）。
			 *
			 * 如果填充值为一个数组，则假定为 callback 方法。
			 */
			'create_autofill' => array
			(
				# 属性名 => 填充值
				# 'is_locked' => 0,
			),

			/**
			 * 指定更新数据库中的对象时，哪些属性的值由下面指定的内容进行覆盖
			 *
			 * 填充值的指定规则同 create_autofill
			 */
			'update_autofill' => array
			(
			),

			/**
			 * 在保存对象时，会按照下面指定的验证规则进行验证。验证失败会抛出异常。
			 *
			 * 除了在保存时自动验证，还可以通过对象的 ::meta()->validate() 方法对数组数据进行验证。
			 *
			 * 如果需要添加一个自定义验证，应该写成
			 *
			 * 'title' => array(
			 *	array(array(__CLASS__, 'checkTitle'), '标题不能为空'),
			 * )
			 *
			 * 然后在该类中添加 checkTitle() 方法。函数原型如下：
			 *
			 * static function checkTitle($title)
			 *
			 * 该方法返回 true 表示通过验证。
			 */
			'validations' => array
			(
			),
		);
	}


/* ------------------ 以下是自动生成的代码，不能修改 ------------------ */

	/**
	 * 开启一个查询，查找符合条件的对象或对象集合
	 *
	 * @static
	 *
	 * @return QDB_Select
	 */
	static function find()
	{
		$args = func_get_args();
		return QDB_ActiveRecord_Meta::instance(__CLASS__)->findByArgs($args);
	}

	/**
	 * 返回当前 ActiveRecord 类的元数据对象
	 *
	 * @static
	 *
	 * @return QDB_ActiveRecord_Meta
	 */
	static function meta()
	{
		return QDB_ActiveRecord_Meta::instance(__CLASS__);
	}


/* ------------------ 以上是自动生成的代码，不能修改 ------------------ */
	
	static function create($requirement)
	{
		$image_requirement = new Image_Requirement();
		$image_requirement->name = $requirement['name'];
		$image_requirement->requirement_user_id = $requirement['requirement_user_id'];
		$image_requirement->type = $requirement['type'];
		$image_requirement->note = trim($requirement['note']);
		$image_requirement->handle_status = Q::ini('custom_flag/image_requirement_handle_status/new/value');
		$image_requirement->create_user_id = CURRENT_USER_ID;
		$image_requirement->create_time = CURRENT_DATETIME;
		$image_requirement->save();
		$image_requirement_id = mysql_insert_id();
		foreach ($requirement['group'] as $g)
		{
			Image_Requirement_Group::create($g, $image_requirement_id);
			$image_requirement_group_id = mysql_insert_id();
			foreach ($g['item'] as $i)
			{
				Image_Requirement_Group_Item::create($i, $image_requirement_group_id);
			}
		}
		Image_Requirement::UpdateProductQuantity($image_requirement_id);
		return array('ack' => SUCCESS, 'message' => '');
	}
	
	static function UpdateProductQuantity($image_requirement_id)
	{
		$total = Image_Requirement_Group_Item::find('image_requirement_group.image_requirement_id=?', $image_requirement_id)
			->joinLeft('image_requirement_group', '', 'image_requirement_group.id=image_requirement_group_item.image_requirement_group_id')
			->getCount();
		QDB::getConn()->execute('update image_requirement set product_quantity=' . $total . ' where id=' . $image_requirement_id);
		return array('ack' => SUCCESS, 'message' => '');
	}
	
	/*
	 * 编辑保存产品需求单
	 * @param requirement
	 */
	static function updateRequirement($requirement)
	{
		$judge_result = Image_Requirement::judgeUpdateRequirement($requirement['id']);
		if ($judge_result['ack'] == SUCCESS)
		{
			$image_requirement = Image_Requirement::find('id=?', $requirement['id'])->getOne();
			$image_requirement->name = $requirement['name'];
			$image_requirement->requirement_user_id = $requirement['requirement_user_id'];
			$image_requirement->type = $requirement['type'];
			$image_requirement->note = trim($requirement['note']);
			$image_requirement->save();
			Image_Requirement_Group::update($requirement['group'], $requirement['id']);
			Image_Requirement::UpdateProductQuantity($requirement['id']);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断是否满足编辑保存产品图片需求单
	 * @param $requirement_id
	 */
	static function judgeUpdateRequirement($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/new/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => '');
			}
		}
	}
	
	/*
	 * 删除需求单
	 * $param $requirement_id
	 */
	static function delete($requirement_id)
	{
		$judge_result = Image_Requirement::judgeDelete($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			Image_Requirement_Group::delete($requirement_id);
			Image_Requirement::meta()->deleteWhere('id=?', $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断需求单是否能够删除
	 * $param $requirement_id 需求单id
	 */
	static function judgeDelete($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/new/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从新单转变为需求确认或拍摄确认
	 * @param $requirement_id
	 * @param $photo_path 拍摄图片路径
	 */
	static function changeHandleStatusFromNewToRequirementConfirmedOrPhotoConfirmed($requirement_id, $photo_path='')
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromNewToRequirementConfirmedOrPhotoConfirmed($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			if ($photo_path)
			{
				QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_confirmed/value') . ',requirement_confirmed_user_id=' . CURRENT_USER_ID . ',requirement_confirmed_time=\'' . CURRENT_DATETIME .'\',photo_path=\'' . mysql_escape_string($photo_path) . '\',photo_confirmed_user_id=' . CURRENT_USER_ID . ',photo_confirmed_time=\'' . CURRENT_DATETIME . '\' where id=' . $requirement_id);
			}
			else
			{
				QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/requirement_confirmed/value') . ',requirement_confirmed_user_id=' . CURRENT_USER_ID . ',requirement_confirmed_time=\'' . CURRENT_DATETIME .'\' where id=' . $requirement_id);
			}
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从新单转变为需求确认或拍摄确认
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromNewToRequirementConfirmedOrPhotoConfirmed($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/new/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从需求确认转变为拍摄接受
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromRequirementConfirmedToPhotoAccepted($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRequirementConfirmedToPhotoAccepted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_accepted/value') . ',photo_accepted_user_id=' . CURRENT_USER_ID . ',photo_accepted_time=\'' . CURRENT_DATETIME .'\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从需求确认转变为拍摄接受
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromRequirementConfirmedToPhotoAccepted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/requirement_confirmed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从拍摄接受转变为拍摄完成
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromPhotoAcceptedToPhotoCompleted($requirement_id, $photo_path)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromPhotoAcceptedToPhotoCompleted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_completed/value') . ',photo_completed_user_id=' . CURRENT_USER_ID . ',photo_completed_time=\'' . CURRENT_DATETIME .'\',photo_path=\'' . mysql_escape_string($photo_path) .'\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从拍摄接受转变为拍摄完成
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromPhotoAcceptedToPhotoCompleted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/photo_accepted/value'))
			{
				if ($requirement['photo_accepted_user_id'] == CURRENT_USER_ID)
				{
					return array('ack' => SUCCESS, 'message' => '');
				}
				else
				{
					return array('ack' => FAILURE, 'message' => 'do not have permission to change handle status');
				}
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从拍摄完成或者返拍完成转变为返拍
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromPhotoCompletedOrRephotoCompletedToRephoto($requirement_id, $rephoto_reason)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromPhotoCompletedOrRephotoCompletedToRephoto($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto/value') . ',rephoto_user_id=' . CURRENT_USER_ID . ',rephoto_time=\'' . CURRENT_DATETIME .'\',rephoto_reason=\'' . mysql_escape_string($rephoto_reason) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从拍摄完成或者返拍完成转变为返拍
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromPhotoCompletedOrRephotoCompletedToRephoto($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/photo_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从返拍转变为返拍接受
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromRephotoToRephotoAccepted($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRephotoToRephotoAccepted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto_accepted/value') . ',rephoto_accepted_user_id=' . CURRENT_USER_ID . ',rephoto_accepted_time=\'' . CURRENT_DATETIME .'\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从返拍转变为返拍接受
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromRephotoToRephotoAccepted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从返拍接受转变为返拍完成
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromRephotoAcceptedToRephotoCompleted($requirement_id, $photo_path)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRephotoAcceptedToRephotoCompleted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto_completed/value') . ',rephoto_completed_user_id=' . CURRENT_USER_ID . ',rephoto_completed_time=\'' . CURRENT_DATETIME .'\',photo_path=\'' . mysql_escape_string($photo_path) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从返拍接受转变为返拍完成
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromRephotoAcceptedToRephotoCompleted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto_accepted/value'))
			{
				if ($requirement['rephoto_accepted_user_id'] == CURRENT_USER_ID)
				{
					return array('ack' => SUCCESS, 'message' => '');
				}
				else
				{
					return array('ack' => FAILURE, 'message' => 'do not have permission to change handle status');
				}
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从拍摄完成或者返拍完成转变为拍摄确认
	 * @param $requirement_id
	 */
	static function ChangeHandleStatusFromPhotoCompletedOrRephotoCompletedToPhotoConfirmed($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromPhotoCompletedOrRephotoCompletedToPhotoConfirmed($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_confirmed/value') . ',photo_confirmed_user_id=' . CURRENT_USER_ID . ',photo_confirmed_time=\'' . CURRENT_DATETIME .'\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从拍摄完成或者返拍完成转变为拍摄确认
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromPhotoCompletedOrRephotoCompletedToPhotoConfirmed($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/photo_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从拍摄确认或者返拍完成转变为设计接受
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromPhotoConfirmedToDesignAccepted($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromPhotoConfirmedToDesignAccepted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_accepted/value') . ',design_accepted_user_id=' . CURRENT_USER_ID . ',design_accepted_time=\'' . CURRENT_DATETIME .'\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从拍摄确认转变为设计接受
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromPhotoConfirmedToDesignAccepted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/photo_confirmed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从或者设计接受转变为设计完成
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromDesignAcceptedToDesignCompleted($requirement_id, $design_path)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromDesignAcceptedToDesignCompleted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_completed/value') . ',design_completed_user_id=' . CURRENT_USER_ID . ',design_completed_time=\'' . CURRENT_DATETIME . '\',design_path=\'' . mysql_escape_string($design_path) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从设计接受转变为设计完成
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromDesignAcceptedToDesignCompleted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/design_accepted/value'))
			{
				if ($requirement['design_accepted_user_id'] == CURRENT_USER_ID)
				{
					return array('ack' => SUCCESS, 'message' => '');
				}
				else
				{
					return array('ack' => FAILURE, 'message' => 'do not have permission to change handle status');
				}
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从设计完成或者返修成功转变为返修
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromDesignCompletedOrRedesignCompletedToRedesign($requirement_id, $redesign_reason)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromDesignCompletedOrRedesignCompletedToRedesign($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign/value') . ',redesign_user_id=' . CURRENT_USER_ID . ',redesign_time=\'' . CURRENT_DATETIME . '\',redesign_reason=\'' . mysql_escape_string($redesign_reason) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从设计完成或者返修成功转变为返修
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromDesignCompletedOrRedesignCompletedToRedesign($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/design_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从返修转变为返修接受
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromRedesignToRedesignAccepted($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRedesignToRedesignAccepted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign_accepted/value') . ',redesign_accepted_user_id=' . CURRENT_USER_ID . ',redesign_accepted_time=\'' . CURRENT_DATETIME . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从返修转变为返修接受
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromRedesignToRedesignAccepted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从返修接受转变为返修完成
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromRedesignAcceptedToRedesignCompleted($requirement_id, $design_path)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRedesignAcceptedToRedesignCompleted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign_completed/value') . ',redesign_completed_user_id=' . CURRENT_USER_ID . ',redesign_completed_time=\'' . CURRENT_DATETIME . '\',design_path=\'' . mysql_escape_string($design_path) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从返修接受转变为返修完成
	 * @param $requirement_id
	 */
	static function judgeChangeHandleStatusFromRedesignAcceptedToRedesignCompleted($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign_accepted/value'))
			{
				if ($requirement['redesign_accepted_user_id'] == CURRENT_USER_ID)
				{
					return array('ack' => SUCCESS, 'message' => '');
				}
				else
				{
					return array('ack' => FAILURE, 'message' => 'do not have permission to change handle status');
				}
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 图片需求单操作状态从设计完成或者返修完成转变为设计确认
	 * @param $requirement_id
	 */
	static function changeHandleStatusFromDesignCompletedOrRedesignCompletedToDesignConfirmed($requirement_id)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromDesignCompletedOrRedesignCompletedToDesignConfirmed($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_confirmed/value') . ',design_confirmed_user_id=' . CURRENT_USER_ID . ',design_confirmed_time=\'' . CURRENT_DATETIME . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断图片需求单操作状态是否可以从设计完成或者返修完成转变为设计确认
	 * @param $requirement_id
	 */
	static function JudgechangeHandleStatusFromDesignCompletedOrRedesignCompletedToDesignConfirmed($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/design_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 更新拍摄图片存放路径
	 */
	static function updatePhotoPath($requirement_id, $photo_path)
	{
		$judge_result = Image_Requirement::judgeUpdatePhotoPath($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set photo_path=\'' . mysql_escape_string($photo_path) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断是否满足更新拍摄图片存放路径
	 */
	static function judgeUpdatePhotoPath($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/photo_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 更新返拍原因
	 */
	static function updateRephotoReason($requirement_id, $rephoto_reason)
	{
		$judge_result = Image_Requirement::judgeUpdateRephotoReason($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set rephoto_reason=\'' . mysql_escape_string($rephoto_reason) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断是否满足更新返拍原因
	 */
	static function judgeUpdateRephotoReason($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/rephoto/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 更新设计图片存放路径
	 */
	static function updateDesignPath($requirement_id, $design_path)
	{
		$judge_result = Image_Requirement::judgeUpdateDesignPath($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set design_path=\'' . mysql_escape_string($design_path) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}

	/*
	 * 判断是否满足更新设计图片存放路径
	 */
	static function judgeUpdateDesignPath($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/design_completed/value') || $requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign_completed/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	/*
	 * 更新返修原因
	 */
	static function updateRedesignReason($requirement_id, $redesign_reason)
	{
		$judge_result = Image_Requirement::judgeChangeHandleStatusFromRedesignToRedesignAccepted($requirement_id);
		if ($judge_result['ack'] == SUCCESS)
		{
			QDB::getConn()->execute('update image_requirement set redesign_reason=\'' . mysql_escape_string($redesign_reason) . '\' where id=' . $requirement_id);
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => $judge_result['message']);
		}
	}
	
	/*
	 * 判断是否满足更新返修原因
	 */
	static function judgeUpdateRedesignReason($requirement_id)
	{
		$requirement = Image_Requirement::find('id=?', $requirement_id)->asArray()->getOne();
		if ($requirement)
		{
			if ($requirement['handle_status'] == Q::ini('custom_flag/image_requirement_handle_status/redesign/value'))
			{
				return array('ack' => SUCCESS, 'message' => '');
			}
			else
			{
				return array('ack' => FAILURE, 'message' => 'image requirement handle status is incorrect');
			}
		}
		else
		{
			return array('ack' => FAILURE, 'message' => 'image requirement is not exists');
		}
	}
	
	static function search($condition, $cp, $ps)
	{
		$columns = array(
			'image_requirement.*',
			'o.name as create_user_name, a.name as requirement_user_name, b.name as requirement_confirmed_user_name, c.name as photo_accepted_user_name, d.name as photo_completed_user_name, e.name as rephoto_user_name, f.name as rephoto_accepted_user_name, g.name as rephoto_completed_user_name, h.name as photo_confirmed_user_name',
			'i.name as design_accepted_user_name, j.name as design_completed_user_name, k.name as redesign_user_name, l.name as redesign_accepted_user_name, m.name as redesign_completed_user_name, n.name as design_confirmed_user_name'
		);
		$where = Image_Requirement::buildSearchWhere($condition);
		$select = Image_Requirement::find($where)
			->joinLeft('user as o', '', 'o.id=image_requirement.create_user_id')
			->joinLeft('user as a', '', 'a.id=image_requirement.requirement_user_id')
			->joinLeft('user as b', '', 'b.id=image_requirement.requirement_confirmed_user_id')
			->joinLeft('user as c', '', 'c.id=image_requirement.photo_accepted_user_id')
			->joinLeft('user as d', '', 'd.id=image_requirement.photo_completed_user_id')
			->joinLeft('user as e', '', 'e.id=image_requirement.rephoto_user_id')
			->joinLeft('user as f', '', 'f.id=image_requirement.rephoto_accepted_user_id')
			->joinLeft('user as g', '', 'g.id=image_requirement.rephoto_completed_user_id')
			->joinLeft('user as h', '', 'h.id=image_requirement.photo_confirmed_user_id')
			->joinLeft('user as i', '', 'i.id=image_requirement.design_accepted_user_id')
			->joinLeft('user as j', '', 'j.id=image_requirement.design_completed_user_id')
			->joinLeft('user as k', '', 'k.id=image_requirement.redesign_user_id')
			->joinLeft('user as l', '', 'l.id=image_requirement.redesign_accepted_user_id')
			->joinLeft('user as m', '', 'm.id=image_requirement.redesign_completed_user_id')
			->joinLeft('user as n', '', 'n.id=image_requirement.design_confirmed_user_id')
			->order('image_requirement.create_time desc')->setColumns($columns);
		if ($cp == 0 && $ps == 0)
		{
			$pagination = array();
		}
		else
		{
			$select = $select->limitPage($cp, $ps);
			$pagination = $select->getPagination();
		}
		$requirement = $select->asArray()->getAll();
		return array(
			'ack' => SUCCESS,
			'message' => '',
			'data' => $requirement,
			'pagination' => $pagination
		);
	}
	
	static function buildSearchWhere($condition)
	{
		$where = '1';
		if (CURRENT_USER_NAME == 'langyi')
		{
			$where .= ' and image_requirement.create_user_id=' . CURRENT_USER_ID;
		}
		if ( ! empty($condition['id']))
		{
			$where .= ' and image_requirement.id=\'' . $condition['id'] . '\'';
		}
		if ( ! empty($condition['create_time_begin']))
		{
			$where .= ' and image_requirement.create_time>=\'' . $condition['create_time_begin'] . '\'';
		}
		if ( ! empty($condition['create_time_end']))
		{
			$where .= ' and image_requirement.create_time<=\'' . $condition['create_time_end'] . ' 23:59:59\'';
		}
		if ( ! empty($condition['requirement_user_id']))
		{
			$where .= ' and image_requirement.requirement_user_id=\'' . $condition['requirement_user_id'] . '\'';
		}
		if ( ! empty($condition['name']))
		{
			$where .= ' and image_requirement.name like \'%' . $condition['name'] . '%\'';
		}
		if ( ! empty($condition['photo_confirmed_time_begin']))
		{
			$where .= ' and image_requirement.photo_confirmed_time>=\'' . $condition['photo_confirmed_time_begin'] . '\'';
		}
		if ( ! empty($condition['photo_confirmed_time_end']))
		{
			$where .= ' and image_requirement.photo_confirmed_time<=\'' . $condition['photo_confirmed_time_end'] . ' 23:59:59\'';
		}
		if ( ! empty($condition['photo_handle_status']))
		{
			$where .= ' and (image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/new/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/requirement_confirmed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_accepted/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_completed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto_accepted/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/rephoto_completed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_confirmed/value') . ')';
		}
		if ( ! empty($condition['design_handle_status']))
		{
			$where .= ' and (image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/photo_confirmed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_accepted/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_completed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign_accepted/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/redesign_completed/value') . ' or image_requirement.handle_status=' . Q::ini('custom_flag/image_requirement_handle_status/design_confirmed/value') . ')';
		}
		if (isset($condition['type']))
		{
			$type_filter = '0';
			foreach (Q::ini('custom_flag/image_requirement_type') as $t)
			{
				if (isset($condition['type'][$t['value']]) && $condition['type'][$t['value']] == $t['value'])
				{
					$type_filter .= ' or image_requirement.type=' . $t['value'];
				}
			}
			$where .= ' and (' . $type_filter . ')';
		}
		if (isset($condition['handle_status']))
		{
			$handle_status_filter = '0';
			foreach (Q::ini('custom_flag/image_requirement_handle_status') as $hs)
			{
				if (isset($condition['handle_status'][$hs['value']]) && $condition['handle_status'][$hs['value']] == $hs['value'])
				{
					$handle_status_filter .= ' or image_requirement.handle_status=' . $hs['value'];
				}
			}
			$where .= ' and (' . $handle_status_filter . ')';
		}
		return $where;
	}
}